#ifndef GB28181_REGISTER_H
#define GB28181_REGISTER_H

#include <string>
#include <mutex>
#include <chrono>
#include <functional>

#include "../gb28181_service.h"

extern "C" {
#include <eXosip2/eXosip.h>
#include <osipparser2/osip_message.h>
}

namespace El {
namespace GB28181 {

class GB28181Register {
public:
    using ServerContext = GB28181Service::ServerContext;

    // Send initial registration request
    static void SendRegister(ServerContext &server);

    // Send registration with authentication
    static void SendRegisterWithAuth(ServerContext &server);

    // Handle registration failure (401 response)
    static void HandleRegisterFailure(eXosip_event_t *event,
                                      ServerContext &server,
                                      std::function<void(const std::string &)> statusCallback);

    // Parse WWW-Authenticate header
    static bool ParseAuthenticateHeader(const std::string &authenticate, ServerContext &server);

    // Build Authorization header for digest authentication
    static std::string BuildAuthorizationHeader(const ServerContext &server,
                                                const std::string &method,
                                                const std::string &uri);

    // Maximum retry count for authentication
    static constexpr int MAX_RETRY_COUNT = 3;
};

} // namespace GB28181
} // namespace El

#endif // GB28181_REGISTER_H
